
from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet
from meiduo_admin.serializers.sku_serializers import *
from meiduo_admin.paginations import MyPage

class SKUGoodsView(ModelViewSet):
    queryset = SKU.objects.all()
    serializer_class = SKUModelSerializer

    pagination_class = MyPage

    def get_queryset(self):
        keywork = self.request.query_params.get('keyword')
        if keywork:
            return self.queryset.filter(name__contains=keywork)
        return self.queryset.all()


class SKUCategorieView(ListAPIView):
    queryset = GoodsCategory.objects.filter(parent_id__gt=37)
    serializer_class = GoodsCateSimpleSerializer

class SPUSimpleView(ListAPIView):
    queryset = SPU.objects.all()
    serializer_class = SPUSimpleSerializer

class SPUSpecView(ListAPIView):
    queryset = SPUSpecification.objects.all()
    serializer_class = SpecSimpleSerializer

    def get_queryset(self):
        # 路径参数中提取spu_id，过滤出这个spu商品所关联的,多个规格数据，序列化返回
        # 如何在非视图函数中，获取路径参数？
        # 答：self.kwargs = {"pk": 1}
        spu_id = self.kwargs.get('pk')
        # 过滤出当前spu关联的所有的规格查询集返回
        return self.queryset.filter(spu_id=spu_id)
